<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="title" label="标题">
				<uni-easyinput placeholder="文章标题" v-model="formData.title" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="content" label="文章内容" required>
				<article-editor v-model="formData.content"></article-editor>
			</uni-forms-item>
			<uni-forms-item name="cover" label="封面图" required>
				<uni-file-picker file-mediatype="image" file-extname="jpg,png" mode="grid"
					return-type="object" v-model="formData.cover" :image-styles="imageStyles"> </uni-file-picker>
			</uni-forms-item>
			<uni-forms-item name="category_id" label="分类">
				<uni-data-checkbox v-model="formData.category_id" collection="category"
					field="name as text, _id as value"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="excerpt" label="文章摘录">
				<textarea class="textarea" placeholder="文章摘录" v-model="formData.excerpt" />
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/news.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'news';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			let formData = {
				"category_id": "",
				"title": "",
				"content": "",
				"excerpt": "",
				"cover": null
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				},
				imageStyles:{
					width:120,
					height:120
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					console.log("res")
					console.log(res)
					this.submitForm(res)
				}).catch(() => {
					uni.hideLoading()
				})
			},

			submitForm(value) {
				// 使用 clientDB 提交数据
				db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
<style lang="scss">
	.uni-app--showleftwindow .uni-container .uni-forms {
		max-width: 100%;
	}

	.uni-easyinput {
		width: 100%;
		max-width: calc(100% - 29px);
	}

	.textarea {
		box-sizing: border-box;
		width: 100%;
		max-width: calc(100% - 29px);
		padding: 12px 15px;
		border: 1px solid #e5e5e5;
		border-radius: 4px;
	}
</style>
